﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using CrossGenLogic;
using System.Drawing;

namespace CrossGenGUI
{
    public class Field : IEnumerable
    {
        private Button[,] buttons;
        private int height, width;
        public IEnumerator GetEnumerator()
        {
            return buttons.GetEnumerator();
        }

        public Field()
        {
            buttons = new Button[0, 0];
        }

        public Field(int height, int width, Generator gen)
        {
            this.width = width;
            this.height = height;
            buttons = new Button[width, height];
            List<int> UsedIndexes = new List<int>();
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    buttons[i, j] = new Button();
                    buttons[i, j].Location = new Point(10 + i * 40, 30 + j * 40);
                    buttons[i, j].Size = new Size(40, 40);
                    buttons[i, j].Font = new Font("Arial", 9);
                    buttons[i, j].FlatStyle = FlatStyle.Flat;
                    buttons[i, j].FlatAppearance.BorderSize = 1;
                    if (gen.GetCharType(i, j) == false)
                    {
                        buttons[i, j].BackColor = Color.White;
                        buttons[i, j].Text = gen.GetChar(i, j).ToString();
                        buttons[i, j].FlatAppearance.BorderColor = Color.Black;
                    }
                    else
                    {
                        buttons[i, j].BackColor = Color.Transparent;

                        try
                        {
                            buttons[i, j].BackgroundImage = Image.FromFile("Resources\\BlackBackGround.jpg");
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show("Ошибка при загрузке ресурсов. " + e.Message);
                        }
                    }
                }
            }
            for (int i = 0; i < gen.WordsCount(); i++)
            {
                if (!UsedIndexes.Contains(gen.GetWordIndex(i)))
                {
                    if (buttons[gen.GetGridElement(i).X, gen.GetGridElement(i).Y].Text == " ")
                        buttons[gen.GetGridElement(i).X, gen.GetGridElement(i).Y].Text = gen.GetWordIndex(i).ToString() + System.Environment.NewLine;
                    else
                        buttons[gen.GetGridElement(i).X, gen.GetGridElement(i).Y].Text = gen.GetWordIndex(i).ToString() + System.Environment.NewLine + buttons[gen.GetGridElement(i).X, gen.GetGridElement(i).Y].Text;
                    UsedIndexes.Add(gen.GetWordIndex(i));
                }
            }

        }

        public void UpdateField(Generator gen)
        {
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    if (buttons[i, j].BackColor == Color.White) buttons[i, j].Text = buttons[i, j].Text + " " + gen.GetChar(i, j);
                }
            }
        }
    }
}
